package com.hanlan.springboot.controller;

import com.hanlan.springboot.util.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: Redis的Controller类
 * @author: 01369542
 * @create: 2018/11/15
 **/
@Slf4j
@RestController
@EnableAutoConfiguration
@RequestMapping("/redis")
public class RedisController {

    private static final Logger logger = LoggerFactory.getLogger(RedisController.class);

    @Autowired
    private RedisUtil redisUtil;
    @RequestMapping("/setStringOfRedis")
    public Object setStringOfRedis(HttpServletRequest request) {
        String key = request.getParameter("key");
        String value = request.getParameter("value");
        logger.info("key={},value={}", key,value);

        if (StringUtils.isEmpty(key)) return key + " does not exist.";
        if (StringUtils.isEmpty(value)) return value + " does not exist.";

        return redisUtil.set(key,value);
    }

    @RequestMapping("/getStringOfRedis")
    public Object getStringOfRedis(HttpServletRequest request) {
        String key = request.getParameter("key");
        logger.info("key={}", key);
        if (StringUtils.isEmpty(key)) return key + " does not exist.";

        String value=(String)redisUtil.get(key);
        if(value==null)return "The value of key("+key+") is null.";
        return value;
    }
}
